package com.kyg.tjtoolmanager;

import com.kyg.tjtoolmanager.db.SQLiteUtils;
import com.kyg.tjtoolmanager.driver.UDevice;
import com.kyg.tjtoolmanager.driver.UHFRFIDDriver;
import com.kyg.tjtoolmanager.utils.SerialPortFinder;
import com.sun.jna.Pointer;
import javafx.fxml.FXML;
import javafx.scene.control.Label;

public class HelloController {
    @FXML
    private Label welcomeText;
    String log = "";

    @FXML
    protected void onHelloButtonClick() {
        welcomeText.setText("Welcome to JavaFX Application!");
        //连接读写器
        SerialPortFinder mSerialPortFinder = new SerialPortFinder();
        String[] serialPorts = mSerialPortFinder.getAllDevicesPath();
        updateText("串口个数：" + serialPorts.length);
        String usbSer = "";
        for (int i = 0; i < serialPorts.length; i++) {
            if (serialPorts[i].contains("USB")) {
                updateText(serialPorts[i]);
                usbSer = serialPorts[i];
                break;
            }
        }

//        HttpService.getInstance().httpGet("http://192.168.3.18/LAPI/V1.0/PeopleLibraries/BasicInfo", null, new HttpBack() {
//            @Override
//            public void onSuccess(Call call, String response) {
//                updateText("Success:" + response);
//            }
//
//            @Override
//            public void onFail(Call call, String e) {
//                updateText("Failed:" + e);
//            }
//        });

        UDevice uDevice = UHFRFIDDriver.rfidDriver.UHF_RFID_Init(usbSer, 115200);
        if (uDevice == null) {
            updateText("连接失败");
            return;
        } else {
            updateText("连接成功：" + uDevice.toString());

        }
        int Status = 1;
        if (uDevice != null) {
            updateText("读写器连接成功 " + Thread.currentThread().getName());
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            UHFRFIDDriver.rfidDriver.UHF_RFID_Reg_InvTagCallBack(uDevice, new UHFRFIDDriver.InventoryTagCallback() {

                @Override
                public void onTagCallback(Pointer data, int length) {
                    try {
                        byte[] result = data.getByteArray(0, length);
                        for (int i = 0; i < length; i++)
                            System.out.print(String.format("%02X ", result[i]));
                        System.out.println("\n");
//                        updateText("返回数据长度："+result.length+"  length："+length);
//                        updateText("onTagCallback data:" + data.getPointer(0).getByteArray(0,length)+ "  length:" + length);
                    } catch (Exception ex) {
                        updateText(ex.getMessage());
                    }
                }
            });

            //开始连续寻标签
            Status = UHFRFIDDriver.rfidDriver.UHF_RFID_IventoryContinuity(uDevice, (short) 0);
            if (Status < 0) {
                updateText("UHF_RFID_IventoryContinuity fail");
            } else {
                updateText("UHF_RFID_IventoryContinuity success");
            }
        }

    }

    private void updateText(String msg) {
        SQLiteUtils.insertLog(msg);
//        welcomeText.setText(welcomeText.getText().toString()+"\n"+msg);
    }

}